--[[
	HPBar is a small statusbar for your current hp.
	The bar is on top of a lego block, so it can be stuck to the RageBar, or any other lego blocks.
]]
local TankIt = LibStub("AceAddon-3.0"):GetAddon("TankIt", true)
if not TankIt then return end

local HPBar = TankIt:NewModule("HPBar", "AceEvent-3.0")

local LegoBlock = LibStub("LegoBlock-Beta1")

function HPBar:OnInitialize()
	local defaults = {
		profile = {
			blockOptions = {
				width = 136,
				height = 22,
			},
		},
	}
	self.db = self.core.db:RegisterNamespace("HPBar", defaults)

	local block = LegoBlock:New("TankItHPBarBlock", nil, nil, self.db.profile.blockOptions)
	block:Hide()
	self.block = block
		
	local bar = CreateFrame("StatusBar", "TankItHPBar", block, "TextStatusBar")
	bar:SetPoint("CENTER")
	bar:SetWidth(134)
	bar:SetHeight(12)
	bar:SetStatusBarTexture("Interface\\TargetingFrame\\UI-StatusBar")
	bar:SetStatusBarColor(0, 1, 0, 1)	-- green
	self.bar = bar
	
	local text = bar:CreateFontString(nil, "ARTWORK", "TextStatusBarText")
	text:SetPoint("CENTER")
	--text:SetText("0/100")
	self.text = text
	
	self:UpdateBar()
end

function HPBar:OnEnable()
	self.block:Show()
	self:RegisterEvent("UNIT_RAGE", "UpdateBar")
	self:RegisterEvent("UNIT_MAXRAGE", "UpdateBar")
	self:RegisterEvent("UNIT_DISPLAYPOWER", "UpdateBar")
end

function HPBar:OnDisable()
	self.block:Hide()
end

function HPBar:UpdateBar()
	local bar = self.bar
	local text = self.text
	
	local hp  = UnitHealth("player")
	local max = UnitHealthMax("player")
	
	bar:SetMinMaxValues(0, max)
	bar:SetValue(hp)
	
	text:SetText(("%d / %d"):format(hp, max))
end

-- debug
--_G.TI_HB = HPBar
